//
//  main.m
//  Province-City-Zone
//
//  Created by 402030900@qq.com on 16/7/11.
//  Copyright © 2016年 402030900@qq.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "provinde.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
   // 第四题

    // 拿到数据源字符串
    NSString * sourceString = [NSString stringWithContentsOfFile:@"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/Province-City-Zone/Province-City-Zone/area.txt" encoding:NSUTF8StringEncoding error:nil];
    // 根据回车分割字符串
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    // 1.
#if 0
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        // 取到所有的省(没有空格开头)
        if (![area hasPrefix:@" "]) {
            //创建了省字典(字典中包含两个字段 name array)
            NSMutableDictionary * proDic = [NSMutableDictionary dictionary];
            //添加proName字段
            [proDic setObject:area forKey:@"proName"];
            //创建省中所有市的数组
            NSMutableArray * cityArray = [NSMutableArray array];
            // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
            // 将创建好的省字典,放到省数组中
            [allProvinceArray addObject:proDic];
        }  else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]){
//            // 难点: 通过lastObject获取到对应的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 取到所有的市(两个空格开头,但不是四个空格开头)
            // 拿到省字典中对应的市数组
          NSMutableArray * cityArray =  [proDic objectForKey:@"cityArray"];
//            [cityArray addObject:area];
            // 创建 市字典(name 和 区array)
            NSMutableDictionary * cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
            // 向市数组中添加市字典
            [cityArray addObject:cityDic];
        }  else{
           //  取到所有的区和县(四个空格开头)
            // 通过省数组获取到当前所在的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
            // 通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            //通过市数组获取到对应的市字典
            NSMutableDictionary *city = [cityArray lastObject];
            //通过市字典获取到对应的区/县的数组
            NSMutableArray *zoneArray = [city objectForKey:@"zoneArray"];
            //最终将区/县添加到数组中
            [zoneArray addObject:area];
        }
    }
    NSLog(@"%@",allProvinceArray);
#endif
#if 1
    // 2. 省数组 -province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            provinde * pro = [provinde zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]){
            provinde * pro = [allProArray lastObject];
            City * city = [City zoneWithName:@"area"];
            [pro.array addObject:city];
        } else {
            City * city = [[[allProArray lastObject] array] lastObject];
             Zone * zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@",allProArray);
#endif
#if 0
    // 第五题
    NSString *locationPath = @"/Users/402030900qq.com/DLS160710/Objective-C/HomeWork/Province-City-Zone/Province-City-Zone/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile: locationPath encoding:NSUTF8StringEncoding error:nil];
    // 根据 , 分割字符串,得到所有城市的数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSLog(@"%@",allCityArray);
    NSMutableArray * allCityDicArray = [NSMutableArray array];
    // 根据每一个城市中的|处理字符串,重新生成字典的数组
    for (NSString *city in allCityArray) {
        // 根据|切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
        // 根据生成的数组,生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject],@"cityName",[arrayCity lastObject],@"cityNumber", nil];
        // 添加到新数组
        [allCityDicArray addObject:cityDic];
    }
    // 排序(重要)
    NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sort]];
//    NSLog(@"%@",allCityDicArray);
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCityDicArray) {
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
        } else if ([cityNumber hasSuffix:@"00"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
//            NSMutableArray * zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject ];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
//    NSLog(@"%@",finalArray);
 /*******************/
    NSMutableArray *allTempArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        // 根据|切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
       TempCity *tempCity = [[TempCity alloc]initWithName:[city firstObject] number:[city lastObject]];
        [allTempArray addObject:tempCity];
    }
    // 对 类数组 排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc]initWithKey:@"cityNumber" ascending:YES];
    [allCityDicArray sortUsingDescriptors:@[sortTempCity]];
    
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity  *tempCity in allTempArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            provinde *pro = [provinde zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"]  && [tempCity.cityNumber hasSuffix:@"0000"]){
            provinde *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [pro.array addObject:city];
        } else {
            provinde *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        }
    }
    
    NSLog(@"%@",finalClassArray);
#endif
    return 0;
}
